﻿using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using T.FAS.Runtime.Action.SPI;
using T.FAS.Runtime.Metadata.Entity;
using T.FAS.Runtime.Base.Entity;

namespace T.FAS.Runtime.Action.Dao
{
    public class DataResultConvert
    {
        public static List<ActionMetaDataView> ConvertToActionMetaDataView(DataSet ds)
        {
            var list = new List<ActionMetaDataView>();
            if (!IsDataSetValid(ds))
                return list;
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                var data = new ActionMetaDataView()
                {
                    ID = Convert.ToString(row["ID"]),
                    Code = Convert.ToString(row["Code"]),
                    Name = Convert.ToString(row["Name"]),
                    LastModifyTime = Convert.ToDateTime(row["LastModifyTime"]),
                };
                list.Add(data);
            }
            return list;
        }
        public static List<ActionPluginInfo> ConvertToActionPluginInfo(DataSet ds)
        {
            var list = new List<ActionPluginInfo>();
            if (!IsDataSetValid(ds))
                return list;
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                var data = new ActionPluginInfo()
                {
                    ID = Convert.ToString(row["ID"]),
                    Code = Convert.ToString(row["Code"]),
                    Name = Convert.ToString(row["Name"]),
                    ActionCode = Convert.ToString(row["ActionCode"]),
                    IsEnable = Convert.ToInt16(row["IsEnable"]).Equals(1),
                    MetaTypeID = Convert.ToString(row["MetaTypeID"]),
                    SortOrder = Convert.ToInt16(row["SortOrder"]),
                    TargetAssembly = Convert.ToString(row["TargetAssembly"]),
                    RemoteInvokeSID = Convert.ToString(row["RemoteInvokeSID"]),
                    IsForceLocal = Convert.ToInt16(row["IsForceLocal"]).Equals(1),
                };
                list.Add(data);
            }
            return list;
        }
        public static List<FASRouter> ConvertToFASRouter(DataSet ds)
        {
            var list = new List<FASRouter>();
            if (!IsDataSetValid(ds))
                return list;
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                var data = new FASRouter()
                {
                    DevAppID = Convert.ToString(row["DevAppID"]),
                    ServiceSetID = Convert.ToString(row["ServiceSetID"]),
                };
                list.Add(data);
            }
            return list;
        }
        public static List<DataBaseInfo> ConvertToDataBaseInfo(DataSet ds)
        {   
            var list = new List<DataBaseInfo>();
            if (!IsDataSetValid(ds))
                return list;
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                var data = new DataBaseInfo()
                {
                    ID = Convert.ToString(row["ID"]),
                    Code = Convert.ToString(row["Code"]),
                    ConnectionKey = Convert.ToString(row["ConfigKey"]),
                    DBType = ConvertToDbType(Convert.ToString(row["DBType"])),
                };
                list.Add(data);
            }
            return list;
        }

        private static DatabaseType ConvertToDbType(string dbType)
        {
            if (string.IsNullOrEmpty(dbType))
                return DatabaseType.MySQL;
            Enum.TryParse(dbType, out DatabaseType databaseType);
            return databaseType;
        }
        private static bool IsDataSetValid(DataSet ds)
        {
            if (ds == null || ds.Tables == null || ds.Tables.Count <= 0)
                return false;
            DataTable dt = ds.Tables[0];
            if (dt == null || dt.Rows == null || dt.Rows.Count <= 0)
                return false;
            return true;
        }
    }
}
